package org.googlecode.viewt.core.tools.ejb;

import java.io.File;

import org.googlecode.viewt.core.tools.JavaSourceParser;
import org.googlecode.viewt.core.tools.LogicDescRepository;

import xjavadoc.SourceClass;
import xjavadoc.XMethod;

public class EJBUtil
{
  public static String firstUpper(String propName)
  {
    return propName.substring(0, 1).toUpperCase() + propName.substring(1);
  }

  public static String shortClassName(String className)
  {
    return className.substring(className.lastIndexOf(46) + 1);
  }

  public static String getPackagePath(String basePath, String className)
  {
    String[] packageSeg = getPackageName(className).split("[.]");
    String path = "";
    for (int i = 0; i < packageSeg.length; ++i)
    {
      if (i != 0)
        path = path + File.separator;
      path = path + packageSeg[i];
    }

    if (basePath != null) {
      path = basePath + File.separator + path;
    }
    return path;
  }

  public static String shortJavaFileName(String className)
  {
    return shortClassName(className) + ".java";
  }

  public static String fullJavaFileName(String basePath, String className)
  {
    return getPackagePath(basePath, className) + File.separator + shortJavaFileName(className);
  }

  public static String getPackageName(String className)
  {
    int index = className.lastIndexOf(46);
    if (index < 0) {
      return null;
    }
    return className.substring(0, index);
  }

  public static String getLocalHomeClass(String prePackName, String projectName, String moduleName, String bsName)
  {
    return prePackName.toLowerCase() + "." + projectName.toLowerCase() + "." + moduleName.toLowerCase() + ".bs.ejb." + firstUpper(bsName) + "LocalHome";
  }

  public static String getLocalInterfaceClass(String prePackName, String projectName, String moduleName, String bsName)
  {
    return prePackName.toLowerCase() + "." + projectName.toLowerCase() + "." + moduleName.toLowerCase() + ".bs.ejb." + firstUpper(bsName) + "LocalInterface";
  }

  public static String getRemoteHomeClass(String prePackName, String projectName, String moduleName, String bsName)
  {
    return prePackName.toLowerCase() + "." + projectName.toLowerCase() + "." + moduleName.toLowerCase() + ".bs.ejb." + firstUpper(bsName) + "RemoteHome";
  }

  public static String getRemoteInterfaceClass(String prePackName, String projectName, String moduleName, String bsName)
  {
    return prePackName.toLowerCase() + "." + projectName.toLowerCase() + "." + moduleName.toLowerCase() + ".bs.ejb." + firstUpper(bsName) + "RemoteInterface";
  }

  public static String getImplementClass(String prePackName, String projectName, String moduleName, String bsName)
  {
    return prePackName.toLowerCase() + "." + projectName.toLowerCase() + "." + moduleName.toLowerCase() + ".bs.ejb.impl." + firstUpper(bsName);
  }

  public static String getBusinessInterfaceClass(String prePackName, String projectName, String moduleName, String bsName)
  {
    return prePackName.toLowerCase() + "." + projectName.toLowerCase() + "." + moduleName.toLowerCase() + ".bs.ejb.client." + firstUpper(bsName) + "Interface";
  }

  public static String getClientHelperClass(String prePackName, String projectName, String moduleName, String bsName)
  {
    return prePackName.toLowerCase() + "." + projectName.toLowerCase() + "." + moduleName.toLowerCase() + ".bs.ejb.client." + firstUpper(bsName) + "Helper";
  }

  public static String serviceBeanName(String projectName, String moduleName, String bsName)
  {
    return projectName + "_" + moduleName + "_" + bsName + "_EJB";
  }

  public static String serviceTargetName(String projectName, String moduleName, String bsName)
  {
    return projectName + "_" + moduleName + "_" + bsName + "_EJB_Target";
  }

  public static String ejbBeanConfigFile(String basePath, String bsName)
  {
    return basePath + File.separator + "bs" + File.separator + bsName + "_EJB.spc.xml";
  }

  public static String ejbJndiName(String projectName, String moduleName, String bsName)
  {
    return projectName + "." + moduleName + "." + bsName + "EJB";
  }

  public static String docContent(String javaDoc)
  {
    if (javaDoc == null) {
      return "";
    }

    if (!(javaDoc.startsWith("/**")))
      return "";
    if (javaDoc.endsWith("\n"))
      javaDoc = javaDoc.substring(0, javaDoc.length() - 1);
    if (!(javaDoc.endsWith("*/"))) {
      return "";
    }
    String content = javaDoc.substring(javaDoc.indexOf("\n") + 1);
    int lastIndex = content.lastIndexOf("\n");
    if (lastIndex > 0) {
      return content.substring(0, lastIndex);
    }
    return content.substring(0, content.length() - 2);
  }

  public static boolean isImplExtendsDataService(String prePackName, String projectName, String moduleName, LogicDescRepository repository)
  {
    SourceClass implClass = repository.getBLImplement();

    String dsClassName = prePackName + "." + projectName + "." + moduleName + ".bl.Base" + firstUpper(moduleName) + "DataService";

    return implClass.isSubclassOf(dsClassName);
  }

  public static String methodSignature(XMethod method, boolean withParam)
  {
    return JavaSourceParser.methodSignature(method, withParam);
  }
}